package com.briup.web.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;

import com.briup.common.util.MyBatisSqlSessionFactory;
import com.briup.service.impl.TblLinkServiceImpl;

/**
 * Servlet implementation class addLink_Servlet
 */
@WebServlet("/jsp/manager/addLink_Servlet")
public class addLink_Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public addLink_Servlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("category.name") ;
		String url = request.getParameter("category.code") ;
		SqlSession openSession = MyBatisSqlSessionFactory.openSession() ;
		try {
			TblLinkServiceImpl tblLinkServiceImpl = new TblLinkServiceImpl() ;
			int i = tblLinkServiceImpl.insertLinkByNameAndUrl(openSession, name, url) ;
			if (i == 1) {
				openSession.commit();
			}else {
				openSession.rollback();
			}
		} catch (Exception e) {
			openSession.rollback();
			System.out.println(e.getCause().toString());
			e.printStackTrace();
		}finally {
			openSession.close();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
